//
//  KABRootTabVC.m
//  kabu
//
//  Created by XiangDongMBP on 2017/1/25.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABRootTabVC.h"
#import "KABExhibitionVC.h"
#import "KABMallHpVC.h"
#import "KABMineVC.h"
#import "KABCosplayVC.h"
#import "KABModel.h"
#import "KABLoginVC.h"
#import "GlobalHeader.h"
#import "ThemeTabBarItem.h"
#import "UIColor+XDHex.h"
#import <RongIMKit/RongIMKit.h>
#import "KABExhDetailVC.h"
#import "KABTicketDetailVC.h"
#import "KABCosplayDetailVC.h"
#import "AppDelegate.h"

@interface KABRootTabVC ()<UITabBarControllerDelegate>
@property (nonatomic, strong) UINavigationController *navMineVC;
@property (nonatomic, strong) UINavigationController *navLoginVC;
@property (nonatomic, strong) UIView *msgRedDot;
@end

@implementation KABRootTabVC

#pragma mark - life cycle
- (void)dealloc {
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KABGlobalNotiUserTokenInvalid object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KABGlobalNotiLogout object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KABGlobalNotiMsgRedDotShow object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 添加通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenInvalidHandle:) name:KABGlobalNotiUserTokenInvalid object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutHandle:) name:KABGlobalNotiLogout object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(msgRedDotHandle:) name:KABGlobalNotiMsgRedDotShow object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushProcessHandle:) name:KABGlobalNotiPushProcess object:nil];
    
    //Cosplay
    KABCosplayVC * cosplayVC = [[KABCosplayVC alloc] init];
    UINavigationController * navCosVC = [[UINavigationController alloc] initWithRootViewController:cosplayVC];
//    navCosVC.tabBarItem.title = @"Cosplay";
//    navCosVC.tabBarItem.image = [UIImage imageNamed:@"rootvc_cosplay"];
//    navCosVC.tabBarItem.selectedImage =[UIImage imageNamed:@"rootvc_cosplay_sel"];
    navCosVC.tabBarItem = [[ThemeTabBarItem alloc] initWithTitle:@"Cosplay"
                                                       imageName:@"rootvc_cosplay"
                                               selectedImageName:@"rootvc_cosplay_sel"];
    
    //展会
    KABExhibitionVC * exhibitionVC = [[KABExhibitionVC alloc] init];
    UINavigationController * navExhibitionVC = [[UINavigationController alloc] initWithRootViewController:exhibitionVC];
//    navExhibitionVC.tabBarItem.title = @"展会";
//    navExhibitionVC.tabBarItem.image = [UIImage imageNamed:@"rootvc_exhibition"];
//    navExhibitionVC.tabBarItem.selectedImage = [UIImage imageNamed:@"rootvc_exhibition_sel"];
    navExhibitionVC.tabBarItem = [[ThemeTabBarItem alloc] initWithTitle:@"展会"
                                                              imageName:@"rootvc_exhibition"
                                                      selectedImageName:@"rootvc_exhibition_sel"];
    
    //商城
    KABMallHpVC * mallVC = [[KABMallHpVC alloc] init];
    UINavigationController * navMallVC = [[UINavigationController alloc] initWithRootViewController:mallVC];
//    navMallVC.tabBarItem.title = @"商城";
//    navMallVC.tabBarItem.image = [UIImage imageNamed:@"rootvc_mall"];
//    navMallVC.tabBarItem.selectedImage = [UIImage imageNamed:@"rootvc_mall_sel"];
    navMallVC.tabBarItem = [[ThemeTabBarItem alloc] initWithTitle:@"商城"
                                                        imageName:@"rootvc_mall"
                                                selectedImageName:@"rootvc_mall_sel"];
    
    //商城
    KABMineVC * mineVC = [[KABMineVC alloc] init];
    UINavigationController * navMineVC = [[UINavigationController alloc] initWithRootViewController:mineVC];
//    navMineVC.tabBarItem.title = @"我";
//    navMineVC.tabBarItem.image = [UIImage imageNamed:@"rootvc_mine"];
//    navMineVC.tabBarItem.selectedImage = [UIImage imageNamed:@"rootvc_mine_sel"];
    navMineVC.tabBarItem = [[ThemeTabBarItem alloc] initWithTitle:@"我"
                                                        imageName:@"rootvc_mine"
                                                selectedImageName:@"rootvc_mine_sel"];
    self.navMineVC = navMineVC;
    
    self.viewControllers = @[navCosVC, navExhibitionVC, navMallVC, navMineVC];
    self.delegate = self;
    [self.tabBar addSubview:self.msgRedDot];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // 启动的时候查看是否有推送
    [self pushProcessHandle:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if (viewController == self.navMineVC) {
        if (![KABModel sharedInstance].isLogin) {
            [self jumpToLoginView];
            return NO;
        }
        return YES;
    }
    return YES;
}

#pragma mark - private methods
- (void)tokenInvalidHandle:(NSNotification *)notification {
    if (self.navLoginVC == [self topPresentedVC]) {
        return;
    }
    
    [self jumpToLoginView];
}

- (void)logoutHandle:(NSNotification *)notification {
    [(UINavigationController *)self.selectedViewController popToRootViewControllerAnimated:NO];
    self.selectedIndex = 0;
    [self jumpToLoginView];
}
- (void)msgRedDotHandle:(NSNotification *)notification {
    NSInteger newMsgCnt = [RCIMClient sharedRCIMClient].getTotalUnreadCount;
    newMsgCnt += [KABModel sharedInstance].mineNewMsg.totalNewMsgCnt;
    self.msgRedDot.hidden = (newMsgCnt > 0) ? NO : YES;
}

- (void)pushProcessHandle:(NSNotification *)notification {
    NSDictionary *dic = ((AppDelegate *)[UIApplication sharedApplication].delegate).pushDictionary;
    if (!dic) {
        return;
    }
    ((AppDelegate *)[UIApplication sharedApplication].delegate).pushDictionary = nil;
    NSDictionary *kabu = dic[@"kabu"];
    if (kabu && [kabu[@"type"] integerValue] == 0) {
        [self jumpToTicketDetailWithOrderId:kabu[@"id"]];
    } else if (kabu && [kabu[@"type"] integerValue] == 1) {
        [self jumpToExhDetailWithExhId:[kabu[@"id"] integerValue]];
    } else if (kabu && [kabu[@"type"] integerValue] == 3) {
        [self jumpToCosplayDetailWithCosId:[kabu[@"id"] integerValue]];
    } else if (kabu && [kabu[@"type"] integerValue] == 4) {
        [self jumpToCosplayDetailWithCosId:[kabu[@"id"] integerValue]];
    } else if (kabu && [kabu[@"type"] integerValue] == 5) {
        [self jumpToCosplayDetailWithCosId:[kabu[@"id"] integerValue]];
    }
}

- (void)jumpToExhDetailWithExhId:(NSInteger)exhId {
    KABExhDetailVC *exhDetailVC = [[KABExhDetailVC alloc] init];
    exhDetailVC.exhibition_id = exhId;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:exhDetailVC];
    UIViewController *topmostVC = [self topViewController];
    [topmostVC presentViewController:nav animated:YES completion:nil];
}

- (void)jumpToTicketDetailWithOrderId:(NSString *)orderId {
    KABTicketDetailVC *ticketVC = [[KABTicketDetailVC alloc] init];
    ticketVC.order_id = orderId;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:ticketVC];
    UIViewController *topmostVC = [self topViewController];
    [topmostVC presentViewController:nav animated:YES completion:nil];
}

- (void)jumpToCosplayDetailWithCosId:(NSInteger)cosId {
    KABCosplayDetailVC *cosDetailVC = [[KABCosplayDetailVC alloc] init];
    cosDetailVC.cosplay_id = cosId;
    cosDetailVC.showCloseBtnFlag = YES;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:cosDetailVC];
    UIViewController *topmostVC = [self topViewController];
    [topmostVC presentViewController:nav animated:YES completion:nil];
}

- (void)jumpToLoginView {
    self.navLoginVC = [[UINavigationController alloc] initWithRootViewController:[[KABLoginVC alloc] init]];
    UIViewController *topmostVC = [self topViewController];
    [topmostVC presentViewController:self.navLoginVC animated:YES completion:nil];
}

- (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

- (UIViewController *)topPresentedVC {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = resultVC.presentedViewController;
    }
    return resultVC;
}

#pragma mark - getters and setters
- (UIView *)msgRedDot {
    if (!_msgRedDot) {
        _msgRedDot = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * 7.0 / 8 + 8, 7, 5, 5)];
        _msgRedDot.backgroundColor = [UIColor xd_colorWithHex:0xD0011B];
        _msgRedDot.layer.masksToBounds = YES;
        _msgRedDot.layer.cornerRadius = 2.5;
        _msgRedDot.hidden = YES;
    }
    return _msgRedDot;
}

@end
